import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../net/dnp_net_config.dart';
import '../../net/dnp_net_manager.dart';
import '../main/dnp_main_controller.dart';
import '../widget/dnp_top_title_widget.dart';

class DnpAccountOutPage extends StatefulWidget {

  final String phText;

  const DnpAccountOutPage({super.key,required this.phText});

  @override
  State<DnpAccountOutPage> createState() => _DnpAccountOutPageState();
}

class _DnpAccountOutPageState extends State<DnpAccountOutPage> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/images/ic_base_bg.webp"),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
          bottom: false,
          child: Column(
            children: [_titleWidget(), _contentWidget()],
          ),
        ),
      ),
    );
  }

  Widget _titleWidget() {
    return DnpTopTitleWidget(
      titleName: "Account cancellation",
    );
  }

  Widget _contentWidget() {
    return Expanded(
      child: Column(
        children: [
          SizedBox(height: 13),
          Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Stack(
                      children: [
                        Image.asset(
                          'assets/images/ic_setting_account_out.webp',
                          width: 347,
                          height: 528,
                        ),
                        Positioned(
                          left: 100,
                          top: 120,
                          child: Text(
                            widget.phText,
                            style: TextStyle(
                              fontSize:20,
                              fontWeight: FontWeight.w500,
                              color: Color(0xFF252630)
                            ),
                          ),
                        )
                      ],
                    )
                  ],
                ),
              )),
          SizedBox(height: 16),
          SizedBox(
            height: 90,
            child: Column(
              children: [
                SizedBox(height: 8),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          isChecked = !isChecked;
                        });
                      },
                      child: Image.asset(
                          width: 18,
                          height: 18,
                          'assets/images/ic_base_${isChecked ? 'checkbox' : 'uncheckbox'}.webp'),
                    ),
                    SizedBox(width: 4),
                    Text(
                      'I have read and accept the above',
                      style: TextStyle(fontSize: 14, color: Color(0xFF252630)),
                    ),
                  ],
                ),
                SizedBox(height: 14),
                GestureDetector(
                  onTap: (){
                    _accountOut();
                  },
                  child: Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(left: 24,right: 24),
                    height: 48,
                    decoration: BoxDecoration(
                        color: Color(0xFF1D4AC8),
                        borderRadius: BorderRadius.circular(16)),
                    child: Text(
                      'Account cancellation',
                      style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: Colors.white),
                    ),
                  ),
                )
              ],
            ),
          ),
          SizedBox(height: 8)
        ],
      ),
    );
  }

  void _accountOut() async {
    if (isChecked == false) {
      DnpPluginManager().showToast("please read and accept the above");
      return;
    }

    DnpPluginManager().showLoading();

    var result = await DnpNetManager().get(
        '/haseye/mumbled'
    );

    DnpPluginManager().hideLoading();

    if (result.success) {
      DnpCacheManager().saveString("ho_phone", "");
      DnpNetConfig().saveKnowledge("");
      _back(context);
    }else {
      DnpPluginManager().showToast(result.kurios ?? "");
    }
  }

  void _back(BuildContext context) {
    Navigator.pop(context);
    Navigator.pop(context);
    Get.find<DnpMainController>().scrollToHomeWidget();
  }
}
